package com.shixun.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shixun.dao.BaseDao;
import com.shixun.dao.ZhyDao;
import com.shixun.entity.FileEntity;
import com.shixun.entity.FpinglunEntity;
import com.shixun.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by haiyan on 2016/12/12.
 */
@Repository("ZhyService")
public class ZhyServiceImpl implements ZhyService {

    @Autowired
    private ZhyDao zhyDao;
    @Autowired
    private FpinglunEntity fpinglunEntity;
    @Autowired
    private FileEntity fileEntity;
    @Autowired
    private UserEntity userEntity;
    /**
     * 存储文章的内容
     * @param fileEntity
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean saveFileService(FileEntity fileEntity) {
        if(fileEntity == null){
            return false;
        }else {
            fileEntity.setUserEntity((UserEntity) BaseDao.getSessions().get("user"));
            fileEntity.setFtime(new Timestamp(System.currentTimeMillis()));   //发布时间
            fileEntity.setCreatetime(new Timestamp(System.currentTimeMillis()));
            fileEntity.setFnumber("0");      //评论数量
            fileEntity.setFdianzan("0");    //点赞数
            fileEntity.setMore1("1");      //和帖子之间的区分
            fileEntity.setState("1");     //文章状态
            fileEntity.setMore2("1");    //文章浏览量
            zhyDao.saveFileDao(fileEntity);
            return true;
        }
    }

    /**
     * 查找所有的文章信息
     * @return
     */
    @Override
    public List<FileEntity> searchAllFileService() {
        List<Object> list = zhyDao.searchFileDao();
        if (list != null && !list.isEmpty()){
            List<FileEntity> list1 = new ArrayList<>();
            for (int i = 0;i < list.size();i++){
                list1.add((FileEntity)list.get(i));
            }
            return list1;
        }
        return null;
    }

    /**
     * 通过id查找一条文章信息
     * @param string
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public FileEntity serarchOneFileService(String string) {
        if ("".equals(string) || string == null){
            return null;
        }else {
            //每当触发点击查询一条文章信息，，浏览量加一
            FileEntity fileEntity = (FileEntity) zhyDao.searchOneDao(string).get(0);
            fileEntity.setMore2(BaseDao.returnAddyi(fileEntity.getMore2()));
            return fileEntity;
        }
    }

    /**
     * 对文章的点赞
     * @param string
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void wenZhangDianZan(String string) {
        BaseDao.getResponse().setCharacterEncoding("utf-8");
        JSONObject json = new JSONObject();
        if (string == null || "".equals(string)){
            json.put("code","您的网络出现问题，请稍后再试");
            System.out.println("你传过来的数据为空");
        }else {
            List<Object> list = zhyDao.searchOneDaoFromPinLun(string);
            if (list.size() > 0){
                json.put("code","你已经点过赞了,不可重复操作");
            }else{
                zhyDao.saveDianZanInfo(string);
                //对相对应的文件点赞列加一
                FileEntity fileEntity = (FileEntity)zhyDao.searchOneDao(string).get(0);
                fileEntity.setFdianzan(BaseDao.returnAddyi(fileEntity.getFdianzan()));
                json.put("code","感谢您的点赞");
            }
        }
        try {
            BaseDao.getResponse().getWriter().write(json.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 存储留言
     * @param
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveLiuYan(String wzId,String lyMain) {
        BaseDao.getResponse().setCharacterEncoding("utf-8");
        if (wzId == null || "".equals(wzId) || lyMain == null || "".equals(lyMain)){
            try {
                BaseDao.getResponse().getWriter().write(JSON.toJSONString("网络出现问题，请稍后再试"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            zhyDao.savePingLunInfo(wzId,lyMain);  //存储评论信息
            FileEntity fileEntity = (FileEntity)zhyDao.searchOneDao(wzId).get(0);
            fileEntity.setFnumber(BaseDao.returnAddyi(fileEntity.getFnumber()));   //评论数量加一
            List<Object> list = zhyDao.selectWenZhangInfo(wzId);
            List<FpinglunEntity> listPL = new ArrayList<>();
            if (list != null && !list.isEmpty()){
                for (int i = 0;i < list.size();i++){
                    if (((FpinglunEntity)list.get(i)).getMore1().equals("11")){  //只选取文章评论使用
                        listPL.add((FpinglunEntity) list.get(i));
                    }
                }
                List<Map> mapList = new ArrayList<>();
                for(FpinglunEntity pinlun : listPL){
                    mapList.add(pinlun.toMap());
                }
                String result = JSON.toJSONString(mapList);
                try {
                    BaseDao.getResponse().getWriter().write(result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 查找所有文章中 点赞量最多的6条信息
     * @return
     */
    @Override
    public List<FileEntity> searchFileSix() {
        List<Object> list = zhyDao.selectWenZhangSix();
        List<FileEntity> fList = new ArrayList<>();
        if (list != null && !list.isEmpty()){
                for (int i = 0;i < list.size();i++){
                    if (((FileEntity)list.get(i)).getMore1().equals("1")){
                        fList.add((FileEntity) list.get(i));
                        if (fList.size() == 8){
                            return fList;
                        }
                    }
                }
            return fList;
        }
        return null;
    }

    /**
     * 通过文章编号查找所有文章评论信息
     */
    @Override
    public List<FpinglunEntity> selectAllPinLun(String s) {
        if (s == null || "".equals(s)){
            return null;
        }else {
            List<Object> list = zhyDao.selectWenZhangInfo(s);
            List<FpinglunEntity> flist = new ArrayList<>();
            for (int i = 0;i < list.size();i++){
                flist.add((FpinglunEntity) list.get(i));
            }
            return flist;
        }
    }

    /**
     * 通过用户的id查找文件   文章
     * @return
     */
    @Override
    public List<FileEntity> selectMyselfFile() {
        List<Object> list = zhyDao.selectAllFile(((UserEntity)BaseDao.getSessions().get("user")).getId());
        List<FileEntity> flistW = new ArrayList<>();
        if (list != null && !list.isEmpty()){
            for (int i = 0;i < list.size();i++){
                if (((FileEntity)list.get(i)).getMore1().equals("1")){
                    flistW.add((FileEntity) list.get(i));
                }
            }
            return flistW;
        }
        return null;
    }

    /**
     * 通过用户的id查找文件  帖子
     * @return
     */
    @Override
    public List<FileEntity> selectMyselfTiezi() {
        List<Object> list = zhyDao.selectAllFile(((UserEntity)BaseDao.getSessions().get("user")).getId());
        List<FileEntity> flistT = new ArrayList<>();
        if (list.size() > 0){
            for (int i = 0;i < list.size();i++){
                if (((FileEntity)list.get(i)).getMore1().equals("0")){
                    flistT.add((FileEntity) list.get(i));
                }
            }
            return flistT;
        }
        return null;
    }

    /**
     * 通过文件编号删除文件（fileEntity）
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean delFile(String string) {
        System.out.println("pppppppppp++::" + string);
        if (string == null || "".equals(string)){
            return false;
        }else {
            fileEntity.setId(string);
            zhyDao.delteFile(fileEntity);
            return true;
        }
    }

    /**
     * 差找所有的有效用户信息
     */
    @Override
    public List<UserEntity> selectAllUser() {
        List<Object> list = zhyDao.selectAllUser();
        List<UserEntity> listUser = new ArrayList<>();
        if (list != null && !list.isEmpty()){
            for (int i = 0;i < list.size();i++){
                if (((UserEntity)list.get(i)).getLoginId().equals("admin")){
                }else {
                    String timeString = ((UserEntity)list.get(i)).getCreatetime().toString().substring(0,10);
                    ((UserEntity)list.get(i)).setMore2(timeString);
                    listUser.add((UserEntity) list.get(i));
                }
            }
            return listUser;
        }
        return null;
    }

    /**
     * 改变用户状态为0
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public List<UserEntity> oneUserInfo(String string) {
        if (string == null || "".equals(string)){
        }else {
            BaseDao.getResponse().setCharacterEncoding("utf-8");
            List<Object> listUser = zhyDao.selectAllUser();
//            for (int i = 0 ;i < listUser.size();i++){
//                System.out.println("state::::" + listUser.get(i));
//            }
            List<UserEntity> list = new ArrayList<>();
            for (int i = 0;i < listUser.size();i++){
                if (((UserEntity)listUser.get(i)).getLoginId().equals("admin")){
                    System.out.println("去掉admin");
                }else if (((UserEntity)listUser.get(i)).getId().equals(string) && ((UserEntity) listUser.get(i)).getState().equals("1")){
//                    list.get(i).setState("0");
                    ((UserEntity) listUser.get(i)).setState("0");
                    list.add((UserEntity) listUser.get(i));
                }else {
                    list.add((UserEntity) listUser.get(i));
                }
            }
            System.out.println("list::::" + list.toString());
            return list;
//            List<Map> mapList = new ArrayList<>();
//            for(UserEntity user : list){
//                mapList.add(user.toMap());
//            }
//            String result = JSON.toJSONString(mapList);
//            System.out.println("这是json::"+ result);
//            try {
//                BaseDao.getResponse().getWriter().write(result);
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }
        return null;
    }

    /**
     * 查找所有的帖子
     * @return
     */
    @Override
    public List<FileEntity> allTizi() {
        List<Object> objectList = zhyDao.searchTieziDao();
        List<FileEntity> list = new ArrayList<>();
        if (objectList != null && !objectList.isEmpty()){
            for (int i = 0;i < objectList.size();i++){
                list.add((FileEntity) objectList.get(i));
            }
            return list;
        }
        return null;
    }

    /**
     * 改变用户状态为1
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public List<UserEntity> changUser(String string) {
        if (string == null || "".equals(string)){
        }else {
            List<Object> listUser = zhyDao.selectAllUser();
            List<UserEntity> list = new ArrayList<>();
            for (int i = 0;i < listUser.size();i++){
                if (((UserEntity)listUser.get(i)).getLoginId().equals("admin")){
                }else if (((UserEntity)listUser.get(i)).getId().equals(string) && ((UserEntity) listUser.get(i)).getState().equals("0")){
                    ((UserEntity) listUser.get(i)).setState("1");
                    list.add((UserEntity) listUser.get(i));
                }else {
                    list.add((UserEntity) listUser.get(i));
                }
            }
            System.out.println("list::::" + list.toString());
            return list;
        }
        return null;
    }
}
